/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility;

import CRUD.ForumHome;
import CRUD.MemberHome;
import CRUD.PostHome;
import CRUD.ThreadHome;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Message;
import xmlMapping.Post;

/**
 *
 * @author Emyzzz
 */
public class PostOperationsUtility {
    // GET tHREAD Markers

    public static Vector<Integer> getThreadMarkers(xmlMapping.Thread th) {

        Set markers = null;
        Vector<Integer> threadMarkers = new Vector();

        try {
            markers = th.getMembers();
            Iterator iterator = markers.iterator();

            while (iterator.hasNext()) {

                xmlMapping.Member member = (xmlMapping.Member) iterator.next();
                threadMarkers.add(member.getMemberId());
            }

        } catch (NullPointerException e) {
        }
        return threadMarkers;

    } //

    public static void addPost(Post post, Integer threadid) {

        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        xmlMapping.Thread thread = new xmlMapping.Thread();
        ThreadHome th = new ThreadHome();
       
        thread = th.findById(threadid);
        Forum forum=new Forum();
        post.setThread(thread);

        thread.setNoofpost(thread.getNoofpost() + 1);
        thread.setPost(post);
        thread.setLastModified(sqlDate);
       
        post.setThread(thread);
        Vector<Integer> markers = new Vector<Integer>();
        markers = getThreadMarkers(post.getThread());
        
       

        //tell the thread markers that is a post added
         
        for (int i = 0; i < markers.size(); i++) {
            Member member = new Member();
            member.setMemberId(markers.get(i));
            System.out.println(markers.get(i));
            Message msg = new Message(member, post.getMember(), post.getTitle(), MessageUtility.POST_ADDED + thread.getTitle(), false, sqlDate);
            new MessageUtility().sendMessage(msg);
        }
        
        forum = thread.getForum();
        forum.setNoofpost(forum.getNoofpost() + 1);
        forum.setPost(post);
         PostHome ph = new PostHome();
        ph.persist(post);
        ph.releaseConnection();

        th.attachDirty(thread);
        th.session.close();
        new ForumHome().attachDirty(forum);
     
    }

    public static Post getPost(int postId) {

        Post post = new PostHome().findById(postId);
        System.out.println("posttttttttttttttttt" + post.getPostId());
        return post;
    }

    public static void deletePost(int postId) {
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        PostHome postHome = new PostHome();
        List posts = null;
        List forumPosts = null;
        Post post = postHome.findById(postId); // now i get the post which i will delete

        xmlMapping.Thread thread = post.getThread();
        Forum forum = thread.getForum();
        //System.out.println(thread.getPost().getPostId());
        // to update lastpost id cell --> table thread
   
        if (thread.getPost().getPostId() == post.getPostId()) {
            posts = postHome.getPosts(post.getThread());
            System.out.println("sizeeeeeeeee" + posts.size());
            posts.remove(post);
            System.out.println("sizeeeeeeeee" + posts.size());
            if (posts.isEmpty()) {
                Post newPost = null;
                thread.setPost(newPost);
            } else {
                Post lastPost = new Post();
                lastPost = postHome.findById(((Post) posts.get(posts.size() - 1)).getPostId());
                post.getThread().setPost(lastPost);
            }
        }
// to update the last post id --> table forum

        if (forum.getPost().getPostId() == post.getPostId()) {
            forumPosts = postHome.getForumPosts();
            System.out.println("all postss size" + forumPosts.size());
            System.out.println("forum sizeeeeeeeee " + forumPosts.size());
            forumPosts.remove(post);
            System.out.println("forum sizeeeeeeeee " + forumPosts.size());
            if (forumPosts.isEmpty()) {

                Post newPost = null;
                forum.setPost(newPost);
                System.out.println("now forumposts empty");
            } else {
                System.out.println("FORUM POSTS not empty");
                Post lastPost = new Post();
                lastPost = postHome.findById(((Post) forumPosts.get(forumPosts.size() - 1)).getPostId());
                //post.getThread().setPost(lastPost);
                forum.setPost(lastPost);
            }
        }

        if (forum.getNoofpost() == 0) {
            System.out.println("no of form posts->0");
            forum.setNoofpost(0);

        } else {
            forum.setNoofpost(forum.getNoofpost() - 1);
        }

        if (thread.getNoofpost() == 0) {
            System.out.println("no of thread posts zero");
            thread.setNoofpost(0);
        } else {
            thread.setNoofpost(thread.getNoofpost() - 1);
        }
        Set members = null;
        post.setMembers(members);
        postHome.releaseConnection();
        ThreadHome threadHome = new ThreadHome();
        thread.setLastModified(sqlDate);
        threadHome.attachDirty(thread);
        new ForumHome().attachDirty(forum);
        PostHome homee = new PostHome();
        homee.delete(post); // remove the post
        homee.releaseConnection();

    }

    public static void editPost(xmlMapping.Post post, Integer mid) {
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        //PostHome m = new PostHome();
        MemberHome mh = new MemberHome();
        Member mem = mh.findById(mid);

        Vector<Integer> markers = new Vector();
        ThreadHome th = new ThreadHome();
        xmlMapping.Thread thread = th.findById(post.getThread().getThreadId());
        markers = getThreadMarkers(thread);
        new PostHome().attachDirty(post);

        Member member = new Member();
        for (int i = 0; i < markers.size(); i++) {

            member.setMemberId(markers.get(i));
            Message msg = new Message(member, mem, thread.getTitle(), thread.getTitle() + MessageUtility.POST_EDITED, false, sqlDate);

            System.out.println(markers.get(i));
            new MessageUtility().sendMessage(msg);
        }



    }


    public static int getCountPosts(int thId){
         PostHome m = new PostHome();
         return m.getCount(thId);
    }

//    public static void main(String[] args) {

    //public static void main(String[] args) {

//        Post post = new Post();
//        post.setBody("postBody"); //body
//      //  post.setLastModified(new Date()); //last mod data
//        post.setRank(0); // rank
//        post.setTitle("postTitle"); //title
//        Member member = new Member();
//        MemberAccountUtility util = new MemberAccountUtility();
//        member = util.getAccountById(1);
//        post.setMember(member);
//        xmlMapping.Thread thread = new xmlMapping.Thread();
//        ThreadOperationsUtility threadUtil = new ThreadOperationsUtility();
//        thread = threadUtil.getThread(22);
//        System.out.println(thread.getBody());
//        post.setThread(thread); // thread
        // addPost(post);
      //  deletePost(23);

        // Post post=getPost(40);
//        System.out.println("yalhwy"+post.getPostId());
//        System.out.println(post.getRank());
//         List forumPosts = new PostHome().getForumPosts();
//            System.out.println("allpostsssssssssssssssssss size"+forumPosts.size());


    //}
}
